package ticTacToe;

public class Referee {
	private Board myBoard;
	Piece piece;

	public Referee(Board board) {
		System.out.println("Referee.Referee()");
		myBoard = board;
	}

	/**
	 * checks if they pieces are horizontally equal
	 * 
	 * @return
	 */
	public boolean isHorizontallyEqual() {
		boolean isHorizontallyEqual = false;

		for (int row = 0; row < myBoard.getMyboard().length; row++) {

			if ((myBoard.getMyboard()[row][0].getSymbol() == myBoard
					.getMyboard()[row][1].getSymbol())
					& (myBoard.getMyboard()[row][1].getSymbol() == myBoard
							.getMyboard()[row][2].getSymbol())) {

				if (myBoard.getMyboard()[row][0].getSymbol() == 'X'
						| myBoard.getMyboard()[row][0].getSymbol() == 'O') {
					isHorizontallyEqual = true;
				}

			}
		}

		return isHorizontallyEqual;

	}

	/**
	 * checks if they pieces are vertically equal
	 * 
	 * @return
	 */
	public boolean isVerticallyEqual() {
		boolean isverticallyEqual = false;

		for (int row = 0; row < myBoard.getMyboard().length; row++) {

			for (int col = 0; col < myBoard.getMyboard().length; col++) {

				if ((myBoard.getMyboard()[0][col].getSymbol() == myBoard
						.getMyboard()[1][col].getSymbol())
						& (myBoard.getMyboard()[1][col].getSymbol() == myBoard
								.getMyboard()[2][col].getSymbol())) {

					if (myBoard.getMyboard()[0][col].getSymbol() == 'X'
							| myBoard.getMyboard()[0][col].getSymbol() == 'O') {
						isverticallyEqual = true;
					}

				}

			}

		}

		return isverticallyEqual;

	}

	/**
	 * checks if they pieces are diagonally equal
	 * 
	 * @return
	 */
	public boolean isDiagonallyEqual() {
		boolean isDiagonallyEqual = false;

		// test DiagonallyEqual from left to right
		if ((myBoard.getMyboard()[0][0].getSymbol() == myBoard.getMyboard()[1][1]
				.getSymbol())
				& (myBoard.getMyboard()[1][1].getSymbol() == myBoard
						.getMyboard()[2][2].getSymbol())) {

			if (myBoard.getMyboard()[0][0].getSymbol() == 'X'
					| myBoard.getMyboard()[0][0].getSymbol() == 'O') {

				isDiagonallyEqual = true;
			}
		}

		// test DiagonallyEqual from Right to left
		if ((myBoard.getMyboard()[0][2].getSymbol() == myBoard.getMyboard()[1][1]
				.getSymbol())
				& (myBoard.getMyboard()[1][1].getSymbol() == myBoard
						.getMyboard()[2][0].getSymbol())) {

			if (myBoard.getMyboard()[1][1].getSymbol() == 'X'
					| myBoard.getMyboard()[1][1].getSymbol() == 'O') {

				isDiagonallyEqual = true;
			}
		}

		return isDiagonallyEqual;

	}

	/**
	 * return true if one player have one
	 * 
	 * @return
	 */
	public boolean isWin() {
		boolean isWin = false;

		if (isDiagonallyEqual() | isHorizontallyEqual()
				| isVerticallyEqual() == true) {
			isWin = true;
		}

		return isWin;

	}

	/**
	 * if no win and no more step back the the game is draw.
	 * 
	 * @return
	 */
	public boolean isDraw() {
		boolean isDraw = false;
		if (!isWin() & !isSlotFree()) {
			isDraw = true;
		}

		return isDraw;

	}

	/**
	 * returns true if there is slotFree
	 * 
	 * @return
	 */
	public boolean isSlotFree() {
		boolean isSlotFree = true;
		if (myBoard.getSlotFree() == 0) {
			isSlotFree = false;
		}
		return isSlotFree;
	}

}
